package com.example.qxfw.ybzz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.qxfw.common.entity.zy.TourFcst;
import com.example.qxfw.ybzz.mapper.TourFcstMapper;
import com.example.qxfw.ybzz.service.TourFcstService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

@Service
public class TourFcstServiceImpl implements TourFcstService {

    @Autowired
    private TourFcstMapper tourFcstMapper;


    @Override
    public TourFcst getMaxTime() {
        return tourFcstMapper.selectList1();
    }

    @Override
    public List<String> getTimeChar(String forecastType, String dateChar) {
        return tourFcstMapper.selectTimeChar(forecastType, Timestamp.valueOf(dateChar));
    }

    @Override
    public List<TourFcst> getList(String dateChar, String timeChar, String nTimes, String nTimes24, String nTimes12, String stationName, String forecastType) {
        LambdaQueryWrapper<TourFcst> queryWrapper = Wrappers.<TourFcst>lambdaQuery()
                .eq(TourFcst::getDatechar, Timestamp.valueOf(dateChar))
                .eq(TourFcst::getTimechar, timeChar)
                .eq(nTimes != null && !nTimes.equals(""), TourFcst::getNtimes, nTimes)
                .eq(stationName != null && stationName.equals(""), TourFcst::getStationname, stationName)
                .eq(TourFcst::getForecasttype, forecastType)
                .and(nTimes24 != null && !nTimes24.equals("") && nTimes12 != null && !nTimes12.equals(""),
                        wq -> wq.eq(TourFcst::getNtimes, nTimes24)).or().eq(TourFcst::getNtimes, nTimes12);
        return tourFcstMapper.selectList(queryWrapper);
    }

    @Override
    public List<TourFcst> getList1(String dateChar1, String dateChar2, String forecastType) {
        return tourFcstMapper.selectList(Wrappers.<TourFcst>lambdaQuery()
                .eq(TourFcst::getForecasttype, forecastType)
                .between(TourFcst::getDatechar, Timestamp.valueOf(dateChar1), Timestamp.valueOf(dateChar2)));
    }

    @Override
    public List<TourFcst> getList2(String dateChar, String timeChar, String nTimes12, String nTimes24, String stationName) {
        return tourFcstMapper.selectList(Wrappers.<TourFcst>lambdaQuery()
                .eq(TourFcst::getDatechar, Timestamp.valueOf(dateChar))
                .eq(TourFcst::getTimechar, timeChar)
                .and(wq -> wq
                        .eq(TourFcst::getNtimes, nTimes12)
                        .or()
                        .eq(TourFcst::getNtimes, nTimes24))
                .like(TourFcst::getStationid, stationName));
    }

    @Override
    public List<TourFcst> listByParam(String dateChar, String timeChar, Integer nTimes1, Integer nTimes2, String stationId) {
        return tourFcstMapper.selectList(Wrappers.<TourFcst>query()
                .select("stationid", "stationname", "Longitude", "Latitude", "rain", "Weather1", "CAST(cast(WindS AS VARCHAR) as numeric(18,1)) as WindS", "CAST(cast(WindD AS VARCHAR) as numeric(18,1)) as WindD", "CAST(cast(Humid AS VARCHAR) as numeric(18,1)) as Humid,maxtemp,mintemp,cloud,cloudlow")
                .lambda()
                .eq(TourFcst::getDatechar, Timestamp.valueOf(dateChar))
                .eq(TourFcst::getTimechar, timeChar)
                .eq(TourFcst::getForecasttype, 12)
                .and(w -> w
                        .eq(TourFcst::getNtimes, nTimes1)
                        .or()
                        .eq(TourFcst::getNtimes, nTimes2))
                .eq(TourFcst::getStationid, stationId)
                .orderByDesc(TourFcst::getStationid, TourFcst::getNtimes));
    }

    @Override
    public Integer delete(String forecastTime, String strTimeChar, String stationID) {
        LambdaQueryWrapper<TourFcst> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TourFcst::getDatechar, forecastTime)
                .eq(TourFcst::getTimechar, strTimeChar)
                .eq(TourFcst::getStationid, stationID);
        int rows = tourFcstMapper.delete(queryWrapper);
        return rows;
    }

    @Override
    public Integer delete1(String stationId, String dateChar, String timeChar) {
        return tourFcstMapper.delete(Wrappers.<TourFcst>lambdaQuery()
                .eq(TourFcst::getStationid, stationId)
                .eq(TourFcst::getDatechar, Timestamp.valueOf(dateChar))
                .eq(TourFcst::getTimechar, timeChar));
    }

    @Override
    public TourFcst save(TourFcst tourFcst) {
        int rows = tourFcstMapper.insert(tourFcst);
        if (rows > 0) {
            return tourFcst;
        } else {
            return null;
        }
    }

    @Override
    public TourFcst update(TourFcst tourFcst) {
        String stationId = tourFcst.getStationid();
        Timestamp dateChar = new Timestamp(tourFcst.getDatechar().getTime());
        String timeChar = tourFcst.getTimechar();
        Integer nTimes = tourFcst.getNtimes();
        Integer forecastType = tourFcst.getForecasttype();
        tourFcst.setStationid(null);
        tourFcst.setDatechar(null);
        tourFcst.setTimechar(null);
        tourFcst.setNtimes(null);
        tourFcst.setForecasttype(null);

        int update = tourFcstMapper.update(tourFcst, Wrappers.<TourFcst>lambdaUpdate()
                .eq(TourFcst::getStationid, stationId)
                .eq(TourFcst::getTimechar, timeChar)
                .eq(TourFcst::getDatechar, dateChar)
                .eq(TourFcst::getNtimes, nTimes)
                .eq(TourFcst::getForecasttype, forecastType));
        if (update > 0) {
            return tourFcst;
        } else {
            return null;
        }
    }
}
